package com.gc.asm.client.exception;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import org.apache.commons.lang3.StringUtils;

import java.util.HashMap;

public class MybatisExample {

    private String orderByClause;
    private boolean distinct;
    private Integer limit;
    private Integer offset;
    private Object parameter;

    /**
     * 初始分页查询所需信息
     * @param pageIndex
     * @param pageSize
     * @param sortBy
     * @param descending
     * @param filter
     * @return
     */
    public static MybatisExample initPagedInfo(String pageIndex, String pageSize, String sortBy, boolean descending, String filter){
        MybatisExample mybatisExample = new MybatisExample();
        if(StringUtils.isNotEmpty(sortBy)){
            if(descending){
                sortBy = sortBy + " desc";
            }
            mybatisExample.setOrderByClause(sortBy);
        }
        if(StringUtils.isNotEmpty(pageIndex)){
            mybatisExample.setOffset(Integer.parseInt(pageIndex));
        }else{
            mybatisExample.setOffset(1);
        }
        if(StringUtils.isNotEmpty(pageSize)){
            mybatisExample.setLimit(Integer.parseInt(pageSize));
        }else{
            mybatisExample.setLimit(0);
        }
        if(StringUtils.isNotEmpty(filter) && !"{}".equals(filter)){
            mybatisExample.setParameter(JSON.parseObject(filter, new TypeReference<HashMap>() {}));
        }
        return mybatisExample;
    }


    public String getOrderByClause() {
        return orderByClause;
    }

    public void setOrderByClause(String orderByClause) {
        this.orderByClause = orderByClause;
    }

    public boolean isDistinct() {
        return distinct;
    }

    public void setDistinct(boolean distinct) {
        this.distinct = distinct;
    }

    public Integer getLimit() {
        return limit;
    }

    public void setLimit(Integer limit) {
        this.limit = limit;
    }

    public Integer getOffset() {
        return offset;
    }

    public void setOffset(Integer offset) {
        this.offset = offset;
    }

    public Object getParameter() {
        return parameter;
    }

    public void setParameter(Object parameter) {
        this.parameter = parameter;
    }
}
